package com.gzs.learn.payment.api.controller;

import com.gzs.learn.inf.BaseResponseDto;
import com.gzs.learn.inf.PageResponseDto;
import com.gzs.learn.payment.inf.dto.web.PaymentChannel;
import com.gzs.learn.payment.inf.dto.web.PaymentList;
import com.gzs.learn.payment.inf.dto.web.req.PaymentChannelSearchDto;
import com.gzs.learn.payment.inf.dto.web.req.PaymentSearchDto;
import com.gzs.learn.payment.service.IPaymentChannelConfigService;
import com.gzs.learn.payment.service.IPaymentWebService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhisong.guan
 * @date 2022/11/1 16:36
 */
@RequiredArgsConstructor
@RequestMapping("/payment/web")
@RestController
@Slf4j
public class PaymentWebController {
    private final IPaymentWebService paymentWebService;

    private final IPaymentChannelConfigService channelConfigService;

    @GetMapping("query/detail/{id}")
    public BaseResponseDto<PaymentList> getBySerial(@PathVariable("id") long id) {
        return paymentWebService.paymentDetail(id);
    }

    @PostMapping("query/search-payment")
    public PageResponseDto<PaymentList> searchPaymentList(@RequestBody PaymentSearchDto paymentSearchDto) {
        return paymentWebService.searchPaymentList(paymentSearchDto);
    }

    @GetMapping("query/channel/{id}")
    public BaseResponseDto<PaymentChannel> getChannel(@PathVariable("id") Long id) {
        return BaseResponseDto.success(channelConfigService.getChannelById(id));
    }

    @GetMapping("query/search-channel")
    public PageResponseDto<PaymentChannel> searchChannelList(@RequestBody PaymentChannelSearchDto channelSearchDto) {
        return channelConfigService.searchList(channelSearchDto);
    }

    @PostMapping("add-channel")
    public BaseResponseDto<Boolean> addPaymentChannel(PaymentChannel paymentChannel) {
        int rows = channelConfigService.insertPaymentChannelConfig(paymentChannel);
        if (rows == 0) {
            return BaseResponseDto.bizError("存在指定渠道的支付配置!");
        }
        return BaseResponseDto.success(true);
    }

    @PostMapping("update-channel")
    public BaseResponseDto<Boolean> updatePaymentChannel(PaymentChannel paymentChannel) {
        int rows = channelConfigService.updatePaymentChannelConfig(paymentChannel);
        if (rows == 0) {
            return BaseResponseDto.bizError("存在指定渠道的支付配置!");
        }
        return BaseResponseDto.success(true);
    }
}
